package com.byd.base;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import com.byd.application.MyApplication;
import com.byd.model.DataManager;
import com.byd.utils.Lg;

public abstract class BasePresenter<T extends IPresenter> {
	protected MyApplication mApplication;
	protected Context mContext;
	protected DataManager mDataManager;
	protected T mView;
	private final Handler mHandler = new Handler(Looper.getMainLooper());

	public BasePresenter() {
		v("new instance");
	}

	private final String getTagStr() {
		return Lg.BASE_TAG + getClass().getSimpleName();
	}

	protected final void v(Object msg) {
		Lg.v(getTagStr(), msg);
	}

	protected final void d(Object msg) {
		Lg.d(getTagStr(), msg);
	}

	protected final void i(Object msg) {
		Lg.i(getTagStr(), msg);
	}

	protected final void w(Object msg) {
		Lg.w(getTagStr(), msg);
	}

	protected final void e(Object msg) {
		Lg.e(getTagStr(), msg);
	}

	protected final void wtf(Object msg) {
		Lg.wtf(getTagStr(), msg);
	}

	@SuppressWarnings("unchecked")
	final void performCreate(Context context, Object t) {
		mContext = context;
		mApplication = (MyApplication) mContext.getApplicationContext();
		mDataManager = DataManager.getInstance();
		mView = (T) t;
		onCreate();
	}

	protected void onCreate() {
		// override
	}

	final void performStart() {
		onStart();
	}

	protected void onStart() {
		// override
	}

	final void performResume() {
		onResume();
	}

	protected void onResume() {
		// override
	}

	final void performActive() {
		onActive();
	}

	/** 生命周期为活动的(onStart && Visible) <b>
	 * <p>
	 * 建议在此处注册EventBus */
	protected void onActive() {
		// nothing
	}

	final void performInactive() {
		onInactive();
	}

	/** 生命周期为不活动的(onStop || Invisible) <b>
	 * <p>
	 * 建议在此处反注册EventBus */
	protected void onInactive() {
		// nothing
	}

	final void performPause() {
		onPause();
	}

	protected void onPause() {
		// override
	}

	final void performStop() {
		onStop();
	}

	protected void onStop() {
		// override
	}

	final void performDestroy() {
		onDestroy();
		mHandler.removeCallbacksAndMessages(null);
		mContext = null;
		mApplication = null;
		mDataManager = null;
		mView = null;
	}

	protected void onDestroy() {
		v("onDestroy");
	}

	@Override
	protected void finalize() throws Throwable {
		i("finalize: " + toString());
		super.finalize();
	}

	final void performUIEvent(UIEvent event) {
		onRecieveUIEvent(event);
	}

	/** 接收到UI事件{@link UIEvent}
	 * 
	 * @param event 收到的事件 */
	protected void onRecieveUIEvent(UIEvent event) {
		// for Override
	}

	/** 发送UI事件{@link UIEvent}，在同一个Activity里注册了接收UI事件的Fragment、Activity可以接收到
	 * 
	 * @param event 需发送的事件
	 * @param filter 类筛选，只有这种类型的类才能接收到 */
	protected final void sendUIEvent(UIEvent event, Class<?>... filter) {
		mView.sendUIEvent(event, filter);
	}

	protected final int getColor(int id) {
		return mContext.getResources().getColor(id);
	}
	
	protected final boolean getBoolean(int id) {
		return mContext.getResources().getBoolean(id);
	}

	protected final float getDimension(int id) {
		return mContext.getResources().getDimension(id);
	}

	protected final int getDimensionPixelOffset(int id) {
		return mContext.getResources().getDimensionPixelOffset(id);
	}

	protected final int getDimensionPixelSize(int id) {
		return mContext.getResources().getDimensionPixelSize(id);
	}

	protected final Drawable getDrawable(int id) {
		return mContext.getResources().getDrawable(id);
	}

	protected final String getString(int id) {
		return mContext.getResources().getString(id);
	}

	protected final String[] getStringArray(int id) {
		return mContext.getResources().getStringArray(id);
	}

	protected final int getInteger(int id) {
		return mContext.getResources().getInteger(id);
	}

	protected final int[] getIntArray(int id) {
		return mContext.getResources().getIntArray(id);
	}

	protected final DisplayMetrics getDisplayMetrics() {
		return mContext.getResources().getDisplayMetrics();
	}

	protected final Configuration getConfiguration() {
		return mContext.getResources().getConfiguration();
	}

	protected final boolean post(Runnable r) {
		return mHandler.post(r);
	}

	protected final boolean postSingle(Runnable r) {
		mHandler.removeCallbacks(r);
		return mHandler.post(r);
	}

	protected final boolean postDelayed(Runnable r, long delayMillis) {
		return mHandler.postDelayed(r, delayMillis);
	}

	protected final boolean postSingleDelayed(Runnable r, long delayMillis) {
		mHandler.removeCallbacks(r);
		return mHandler.postDelayed(r, delayMillis);
	}

	protected final void removeCallbacks(Runnable r) {
		mHandler.removeCallbacks(r);
	}
}
